package top.jolyoulu.core.wc

import org.apache.avro.SchemaBuilder.array
import org.apache.spark.api.java.JavaPairRDD
import org.apache.spark.api.java.JavaRDD.fromRDD
import org.apache.spark.{SparkConf, SparkContext}

import java.{lang, util}
import scala.collection.convert.ImplicitConversions.`iterable AsScalaIterable`

/**
 * @Author: JolyouLu
 * @Date: 2023/9/2 22:20
 * @Description
 */
object Spark01_WordCount {

  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf().setMaster("local").setAppName("WordCount")
    val sc = new SparkContext(sparkConf)

    val lines = sc.textFile("datas")

    val words = lines.flatMap(_.split(" "))

    val wordGroup= words.groupBy(word => word)

    val wordToCount = wordGroup.map {
      case(word,list) => {
        (word,list.size)
      }
    }

    val list = wordToCount.collect()

    list.foreach(println)

    sc.stop()
  }

}
